package com.example.dangerouscargodriver.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.entry.CompanyInfo;
import com.example.dangerouscargodriver.entry.EmployeeInfo;
import com.example.dangerouscargodriver.ui.activity.EmployeeInfoActivity;

import java.util.ArrayList;
import java.util.List;

public class EmployeeInfoAdapter extends RecyclerView.Adapter<EmployeeInfoAdapter.ViewHolder>{

	private List<EmployeeInfo> mEmployeeInfoItemItemList = null;
	private Context mContext;
	private ClickListener mClickListener;
	private View mRootView;

	static class ViewHolder extends RecyclerView.ViewHolder {
		private TextView mEmployeeName = null;
		private TextView mEmployeeTelephone = null;

		public ViewHolder(View view) {
			super(view);
			mEmployeeName = view.findViewById(R.id.tv_employee_name);
			mEmployeeTelephone = view.findViewById(R.id.tv_employee_telephone);
		}

	}

	public EmployeeInfoAdapter() {
		mEmployeeInfoItemItemList = new ArrayList<>();
		mClickListener = new ClickListener();
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		mRootView = LayoutInflater.from(parent.getContext()).inflate(
				R.layout.layout_employee_info_item, parent, false);
		ViewHolder holder = new ViewHolder(mRootView);

		mContext = parent.getContext();

		return holder;
	}

	@Override
	public void onBindViewHolder(ViewHolder holder, int position) {
		final int selectPos = position;
		holder.mEmployeeTelephone.setText(mEmployeeInfoItemItemList.get(position).getAccount());
		holder.mEmployeeName.setText(mEmployeeInfoItemItemList.get(position).getName());

		holder.itemView.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				mClickListener.onClick(selectPos);
				notifyDataSetChanged();
			}
		});
	}

	@Override
	public int getItemCount() {

		return mEmployeeInfoItemItemList.size();
	}

	public class ClickListener {
		private int mPosition = -1;

		void onClick(int position){
			mPosition = position;
			Intent intent = new Intent(mRootView.getContext(), EmployeeInfoActivity.class);
			intent.putExtra("info", mEmployeeInfoItemItemList.get(position));
			mRootView.getContext().startActivity(intent);
		}

		public int getPosition() {

			return mPosition;
		}

		public void setPosition(int mPosition) {

			this.mPosition = mPosition;
		}
	}

	public void addData(int position, EmployeeInfo item) {
		mEmployeeInfoItemItemList.add(position, item);
		notifyItemInserted(position);
	}

	public void removeData(int position) {
		mEmployeeInfoItemItemList.remove(position);
		notifyItemRemoved(position);
	}

	public void updateData(int position, EmployeeInfo item) {
		mEmployeeInfoItemItemList.set(position, item);
		notifyItemChanged(position);
	}

	public void clearData(){
		mEmployeeInfoItemItemList.clear();
		notifyDataSetChanged();
	}

	public void appendData(List<EmployeeInfo> array) {
		for(int index = 0; index < array.size(); index++){
			mEmployeeInfoItemItemList.add(array.get(index));
		}
		notifyDataSetChanged();
	}
}
